<template>
  <div>
    <Category :scene="scene" />
    <el-card style="margin: 10px 0">
      <!-- SPU列表 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          icon="plus"
          :disabled="!categoryStore.category3Id"
          @click="handleAddSPU"
        >
          添加SPU
        </el-button>
        <el-table style="margin: 10px 0" :data="recordsList" border>
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                type="success"
                size="default"
                icon="plus"
                title="添加SKU"
                @click="handleAdd(row)"
              ></el-button>
              <el-button
                type="primary"
                size="default"
                icon="edit"
                title="修改SPU"
                @click="handleEdit(row)"
              ></el-button>
              <el-button
                type="warning"
                size="default"
                icon="view"
                title="查看SPU"
                @click="handleView(row)"
              ></el-button>
              <el-popconfirm
                :title="`是否确定删除${row.spuName}？`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="default"
                    icon="delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
        />
      </div>
      <!-- SPU新增 -->
      <SpuForm
        ref="SpuFormRef"
        v-show="scene == 1"
        @changeScene="changeScene"
      />
      <!-- SKU新增 -->
      <SkuForm
        ref="SkuFormRef"
        v-show="scene == 2"
        @changeScene="changeScene"
      />
      <!-- dialog对话框 -->
      <el-dialog title="SKU列表" v-model="skuDialogVisibel">
        <el-table :data="skuList" border>
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="Spu">
import { ref, watch, onBeforeUnmount } from 'vue'
import useCategoryStore from '@/store/modules/category'
import {
  getSkuInfoListApi,
  getSpuListApi,
  removeSpuApi,
} from '@/api/product/spu'
import type {
  SpuResponseData,
  Records,
  SpuData,
  SkuData,
} from '@/api/product/spu/type'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import { ElMessage } from 'element-plus'
const categoryStore = useCategoryStore()
const SpuFormRef = ref()
const SkuFormRef = ref()

const scene = ref<number>(0) // 0显示SPU列表 1显示添加或修改SPU 2显示添加SKU
const currentPage = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref<number>(0)
const recordsList = ref<Records>([]) // SPU数据列表
const skuList = ref<SkuData[]>() // SKU数据列表
const skuDialogVisibel = ref<boolean>(false)

watch(
  () => categoryStore.category3Id,
  () => {
    if (categoryStore.category3Id) {
      getSpuList()
    }
  },
)

// 获取Spu数据列表
const getSpuList = async (pager = 1) => {
  currentPage.value = pager
  const res: SpuResponseData = await getSpuListApi(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id,
  )
  if (res.code == 200) {
    recordsList.value = res.data.records
    total.value = res.data.total
  }
}
// 添加SPU按钮点击事件
const handleAddSPU = () => {
  scene.value = 1

  SpuFormRef.value.initAddSpu(categoryStore.category3Id)
}
// 分页大小改变事件
const handleSizeChange = () => {
  getSpuList()
}
// 添加按钮事件
const handleAdd = (row: SpuData) => {
  scene.value = 2
  // 调用子组件初始化方法
  SkuFormRef.value.initSkuData(
    categoryStore.category1Id,
    categoryStore.category2Id,
    row,
  )
}
// 编辑按钮事件
const handleEdit = (row: SpuData) => {
  scene.value = 1

  SpuFormRef.value.initSpuData(row)
}
// 查看按钮事件
const handleView = async (row: SpuData) => {
  const res = await getSkuInfoListApi(row.id)
  if (res.code == 200) {
    skuList.value = res.data
    skuDialogVisibel.value = true
  }
}
// 删除按钮事件
const handleDelete = async (row: SpuData) => {
  const res: any = await removeSpuApi(row.id)
  if (res.code == 200) {
    getSpuList(
      recordsList.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
    ElMessage.success('删除成功')
  } else {
    ElMessage.error('删除失败')
  }
}
// 切换场景事件
const changeScene = (obj: any) => {
  scene.value = obj.scene
  // 再次获取全部SPU
  if (obj.pageFlag == 'update') {
    getSpuList(currentPage.value)
  } else {
    getSpuList()
  }
}

onBeforeUnmount(() => {
  // 清空仓库中关于分类的数据
  categoryStore.$reset()
})
</script>

<style scoped lang="scss"></style>
